﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Aj.Core;

public partial class Manager_LinkEdit : AuthPageBase
{
    protected int id;
    protected string Title = "";
    protected string LinkUrl = "";
    protected string Body = "";
    protected int Sort = 1;

    protected DataSet ds = null;
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        try
        {
            id = QueryParamInt32("id");
            if (id > 0)
            {
                ds = CustomQuery.Link_GetLinkById(id);
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    Title = ds.Tables[0].Rows[0]["Title"].ToString();
                    LinkUrl = ds.Tables[0].Rows[0]["LinkUrl"].ToString();
                    Body = ds.Tables[0].Rows[0]["Body"].ToString();
                    Sort = ds.Tables[0].Rows[0]["Sort"].ToInt(1);
                }
            }
            else
            {
                object obj = CustomQuery.Link_GetMaxSort();
                Sort = DBNull.Value != obj && null != obj ? obj.ToInt(1) + 1 : 1;
            }

            if (IsPost)
            {
                // 更新
                if (id > 0)
                {
                    if (ds != null && ds.Tables.Count > 0)
                    {
                        int rel = CustomQuery.Link_Update(id, FormParam("Title"), FormParam("LinkUrl"), FormParam("Body"), FormParamInt32("Sort", 1));
                        if (rel > 0)
                        {
                            Page_GoTo("LinkList.aspx", true);
                        }
                        else
                        {
                            MessageBox("修改失败！");
                        }
                    }
                }
                else // 新建
                {
                    // 先检查一下
                    ds = CustomQuery.SysModule_GetModuleById(FormParam("mid"));
                    if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        MessageBox("该id[" + FormParam("mid") + "]已经存在了！");
                    }
                    else
                    {
                        int rel = CustomQuery.Link_Add(FormParam("Title"), FormParam("LinkUrl"), FormParam("Body"), User.Identity.Name, FormParamInt32("Sort", 1));
                        if (rel > 0)
                        {
                            Page_GoTo("LinkList.aspx", true);
                        }
                        else
                        {
                            MessageBox("新建失败！");
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Utility.WriteErrorLog(ex);
        }
    }
}